# encoding: utf-8
"""
@author: 夏洛
@QQ: 1972386194
@file: 02-lianjia.py
"""
import requests,json

class Spider_data():
    def __init__(self):
        self.headers ={
    "Accept": "application/json, text/javascript, */*; q=0.01",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-TW;q=0.6",
    "Cache-Control": "no-cache",
    "Connection": "keep-alive",
    "Host": "cs.fang.lianjia.com",
    "Pragma": "no-cache",
    "Referer": "https://cs.fang.lianjia.com/loupan/pg2/",
    "Sec-Fetch-Dest": "empty",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Site": "same-origin",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36"
}

    def http(self,url):
        res = requests.get(url,headers=self.headers)
        if res.status_code == 200:
            return res

    def get_data(self,url):
        res = self.http(url).json()
        items = res.get("data").get('list')
        for item in items:
            data = {}
            data['city_name'] = item.get('city_name')
            data['district'] = item.get('district')
            data['title'] = item.get('title')
            data['show_price_info'] = item.get('show_price_info')
            self.save_data(data)

    def save_data(self,data):
        with open('data.json','a',encoding='utf-8') as f:
            json.dump(data, f, ensure_ascii=False, indent=4)
            f.write(',')

    def run(self):
        for i in range(1,5):
            url = f'https://cs.fang.lianjia.com/loupan/pg{i}/?_t=1'
            self.get_data(url)

if __name__ == '__main__':
    Spider_data().run()